package com.zhang.zhangapp.view.mine;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.zhang.zhangapp.LoginActivity;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.Constant;
import com.zhang.zhangapp.base.config.BaseConfig;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.user.UserDao;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.utils.CreateMD5;
import com.zhang.zhangapp.utils.T;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;

import org.w3c.dom.Text;
import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/12.
 */

@ContentView(R.layout.activtiy_setting_password)
public class SetPwdActivity extends BaseAppCompatActivityActivity{

    @ViewInject(R.id.setting_code_text)
    TextView codeText;
    @ViewInject(R.id.get_code)
    TextView getCodeText;
    @ViewInject(R.id.setting_password_text)
    TextView pwdText;
    @ViewInject(R.id.action_bar_title)
    TextView titleText;

    private TimeCount time;

    private String setType;

    private String title;

    MCallback getCodeCallBack = new MCallback() {
        @Override
        public void onSuccess(String data) {
            time.start();
        }

        @Override
        public void onSuccess(String message, String code) {
            T.showShort(SetPwdActivity.this, message);
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            T.showShort(SetPwdActivity.this, ex.getMessage().toString());
        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    };

    MCallback setPwdCallBack = new MCallback() {
        @Override
        public void onSuccess(String data) {
            if(setType.equals(Constant.设置支付密码)){
                MApplication.getInstance().getUser().setPaysec(pwdText.getText().toString());
            }else {
                MApplication.getInstance().getUser().setLoginsec(pwdText.getText().toString());
            }
            try{
                UserDao.getInstance().save(MApplication.getInstance().getUser());
            }catch (Exception e){}

            T.showShort(SetPwdActivity.this,title+"修改成功");
            SetPwdActivity.this.finish();
        }

        @Override
        public void onSuccess(String message, String code) {

        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {

        }

        @Override
        public void onFinished() {
            dismisionDalog();
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setType = getIntent().getStringExtra("type");
        title = getIntent().getStringExtra("title");
        x.view().inject(this);
        titleText.setText(title);
        time = new TimeCount(60000, 1000);
    }


    /*
  获取短信验证码
   */
    @Event(type = View.OnClickListener.class, value = R.id.get_code)
    private void getCode(View view) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("mobileNum", MApplication.getInstance().getUser().getMobilenum());
        params.put("type", setType);
        params.put("secretKey", CreateMD5.getMD5(MApplication.getInstance().getUser().getMobilenum() + setType + BaseConfig.SECRETKEY));
        CommonHttp.commonHttp("hzg/sms/sendCode", params, getCodeCallBack);
    }

    @Event(type = View.OnClickListener.class,value = R.id.action_bar_left_image)
    private void back(View view){
        this.finish();
    }

    @Event(type = View.OnClickListener.class,value = R.id.setting_pwd_sure)
    private void sure(View view){
        if(showLoadingDialog()){
            Map<String, String> params = new HashMap<String, String>();
            params.put("memberId", MApplication.getInstance().getUser().getMemberId());
            params.put("optype", setType.equals("2")?"1":"0");
            params.put("sec", pwdText.getText().toString());
            params.put("code", codeText.getText().toString());
            CommonHttp.commonHttp("hzg/member/settingSec", params, setPwdCallBack);
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            getCodeText.setText("获取验证码");
            getCodeText.setEnabled(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            getCodeText.setEnabled(false);
            getCodeText.setText(millisUntilFinished / 1000 + "秒");
        }
    }
}
